package com.api.gbhg.huangwm.BxActionV2;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.api.gbhg.huangwm.dao.YuSuanTiaoZhengDao;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.customization.commons.LogTool;
import com.google.common.base.Joiner;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;

import java.util.*;

/**
 * @Author Huangwm
 * @Date 2022-11-07
 * @Version 1.0
 * @Description TODO
 **/
public class BxActionUtils {
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/BxActionV2/BxActionUtils",false);

    final String reduceJingFeiSQL = "update uf_jingfei set dj=isnull(dj,0)+?,syed=syed-? where lcjlid=? and bmzcjjfl=? ";
    final String releaseJingFeiSQL = "update uf_jingfei set dj=isnull(dj,0)-?,syed=syed+? where lcjlid=? and bmzcjjfl=? ";
    final String reduceSydwedSQL = "update uf_ysxmsydwfped set djje=isnull(djje,0)+?,syed=syed-? where xmbzlc=? and sydw=? ";
    final String releaseSydwedSQL = "update uf_ysxmsydwfped set djje=isnull(djje,0)-?,syed=syed+? where xmbzlc=? and sydw=? ";

    /**
     * 获取预算项目经费主信息
     * @param ysxm
     * @param fieldid
     * @return
     */
    public String getProjectInfo (String ysxm,String fieldid){
        RecordSet rs = new RecordSet();
        rs.execute("select "+fieldid+" from uf_jingfei_version where lcjlsqjlid="+ysxm);
        rs.next();
        return rs.getString("fieldid");
    }
    /**
     * 发起后检查额度获取明细1报销项目数据 group by 合并同类项
     * @param dtTable
     * @param mainid
     * @return
     */
    public List<Map<String,Object>> getDetailsInfo_checkCreate(String dtTable,int mainid){
        List<Map<String,Object>> datas = new ArrayList<>();
        RecordSet rs = new RecordSet();
        //有可能存在冲销金额 因此实际申请的报销金额要减去冲销金额
        rs.execute("select sum(sqbxje-isnull(cxje,0))as sqbxje,ysxm,bmjjzcfl from " +
                dtTable+" where mainid="+mainid +" group by ysxm,bmjjzcfl");
        while (rs.next()){
            Map<String,Object> data = new HashMap<>();
            data.put("ysxm",rs.getString("ysxm"));
            data.put("bmjjzcfl",rs.getString("bmjjzcfl"));
            data.put("sqbxje",rs.getDouble("sqbxje"));
            datas.add(data);
        }
        return datas;
    }

    /**
     * 报销发起后冻结经费表额度
     * @param rst
     * @param datas
     * @return
     * @throws Exception
     */
    public boolean reduceJingFei(RecordSetTrans rst,List<Map<String,Object>> datas) throws Exception {
        logTool.writeLog("报销发起后冻结经费表额度");
        boolean result = false;
        for(int i=0;i<datas.size();i++){
            Map<String,Object> data = datas.get(i);
            boolean thisResult = rst.executeUpdate(reduceJingFeiSQL,data.get("sqbxje"),data.get("sqbxje"),data.get("ysxm"),data.get("bmjjzcfl"));
            logTool.writeLog("reduceJingFei>"+thisResult);
            if(!thisResult){
                return false;
            }
            result = thisResult;
        }
        return result;
    }
    /**
     * 报销发起后冻结使用单位额度
     * @param rst
     * @param datas
     * @return
     * @throws Exception
     */
    public boolean reduceSydwed(RecordSetTrans rst,List<Map<String,Object>> datas,int subid) throws Exception {
        logTool.writeLog("报销发起冻结使用单位额度 subid:"+subid);
        logTool.writeLog(JSONObject.toJSONString(datas));
        boolean result = false;
        for(int i=0;i<datas.size();i++){
            Map<String,Object> data = datas.get(i);
            boolean thisResult = rst.executeUpdate(reduceSydwedSQL,data.get("sqbxje"),data.get("sqbxje"),data.get("ysxm"),subid);
            logTool.writeLog("reduceSydwed>"+thisResult);
            if(!thisResult){
                return false;
            }
            result = thisResult;
        }
        return result;
    }

    /**
     * 报销退回后释放经费表额度
     * @param rst
     * @param datas
     * @return
     * @throws Exception
     */
    public boolean releaseJingFei(RecordSetTrans rst,List<Map<String,Object>> datas) throws Exception {
        boolean result = false;
        for(int i=0;i<datas.size();i++){
            Map<String,Object> data = datas.get(i);
            boolean thisResult = rst.executeUpdate(releaseJingFeiSQL,data.get("sqbxje"),data.get("sqbxje"),data.get("ysxm"),data.get("bmjjzcfl"));
            System.err.println("releaseJingFei>"+thisResult);
            if(!thisResult){
                return false;
            }
            result = thisResult;
        }
        return result;
    }
    /**
     * 报销发起后冻结使用单位额度
     * @param rst
     * @param datas
     * @return
     * @throws Exception
     */
    public boolean releaseSydwed(RecordSetTrans rst,List<Map<String,Object>> datas,int subid) throws Exception {
        boolean result = false;
        for(int i=0;i<datas.size();i++){
            Map<String,Object> data = datas.get(i);
            boolean thisResult = rst.executeUpdate(releaseSydwedSQL,data.get("sqbxje"),data.get("sqbxje"),data.get("ysxm"),subid);
            System.err.println("releaseSydwed>"+thisResult);
            if(!thisResult){
                return false;
            }
            result = thisResult;
        }
        return result;
    }

    /**
     * 发起后同步明细数据至uf_bxsjjl
     * @param rst
     * @param dtTable
     * @param mainid
     * @param requestid
     * @return
     */
    public boolean syncDetailsRecords(RecordSetTrans rst,String dtTable,int mainid,int requestid) throws Exception {
        List<Map<String,Object>> datas = new ArrayList<>();
        RecordSet rs = new RecordSet();
        rs.execute("select * from " + dtTable+" where mainid="+mainid);
        while (rs.next()){
            Map<String,Object> data = new HashMap<>();
            //流程类型 0:主表同步,1:明细表同步
            String lclx = "1";
            //流程id:requestid
            //明细id
            String mxid = rs.getString("id");
            //预算项目id
            String ysxm = rs.getString("ysxm");
            //申请报销金额
            String sqbxje = rs.getString("sqbxje");
            //同意报销金额
            String tybxje = rs.getString("tybxje");
            //部门经济分类
            String bmjjzcfl = rs.getString("bmjjzcfl");
            //政府经济分类
            String zfjjzcfl = rs.getString("zfjjzcfl");
            //是否已提交  0:未提交,1:已提交 默认为未提交
            String sfytj = "1";
            String cxje = rs.getString("cxje");
            data.put("lclx", lclx);//流程类型
            data.put("lcid", requestid);//流程id
            data.put("mxid", mxid);//明细id
            data.put("ysxm", ysxm);//预算项目id
            data.put("sqbxje", sqbxje);//申请报销金额
            data.put("tybxje", tybxje);//同意报销金额
            data.put("bmjjfl", bmjjzcfl);//部门经济支出分类
            data.put("zfjjfl", zfjjzcfl);//政府经济支出分类
            data.put("sfytj", sfytj);//是否已提交
            data.put("cxje", cxje);//冲销金额
            datas.add(data);
        }
        //先删除本流程报销记录
        rs.execute("delete from uf_bxsjjl where lcid="+requestid);
        return storageDataToRecords(rst,datas);
    }

    /**
     * 执行同步报销数据到记录表
     * @param rst
     * @param datas
     * @return
     * @throws Exception
     */
    public boolean storageDataToRecords(RecordSetTrans rst,List<Map<String,Object>> datas) throws Exception {
        for (int i = 0; i < datas.size(); i++) {
            Map<String,Object> data = datas.get(i);
            Set set = data.keySet();
            List<String> listField = new ArrayList<String>(set);
            // 报销数据
            Collection<Object> valueCollection = data.values();
            List<Object> valueList = new ArrayList<>(valueCollection);
            // 存储数据到报销数据记录表
            String uf_baoxiaoshujusql = VersionFlowUtil.getInsertSql(listField, "uf_bxsjjl");
            boolean result = rst.executeUpdate(uf_baoxiaoshujusql, valueList.toArray());
            if(!result){
                return false;
            }
        }
        return true;
    }

    /**
     * 根据id获取项目核批渠道记录中的财政拨款
     * @param xzkmdm
     * @return
     */
    public Map<String,String> getQudaoYsly(int xzkmdm){
        Map<String,String> data = new HashMap<>();
        RecordSet rs = new RecordSet();
        rs.execute("select * from uf_gxmdygnkm where id="+xzkmdm);
        if(rs.next()){
            data.put("xmlcid",rs.getString("xmlcid"));
            data.put("hpssjid",rs.getString("hpssjid"));//核批数据id
            data.put("zfqd",rs.getString("zfqd"));//表里的zfqd为预算来源
            data.put("kmjxj",rs.getString("kmjxj"));//科目级下级
            data.put("kmdm",rs.getString("kmdm"));//科目代码
        }
        return data;
    }

    /**
     * 获取报销项目的总申请金额 用于会计审核时使用单位额度判断的操作
     * @param dtTable
     * @param mainid
     * @return
     */
    public List<Map<String,Object>> getDetailsInfo_checkAuditSyeded(String dtTable,int mainid){
        List<Map<String,Object>> datas = new ArrayList<>();
        RecordSet rs = new RecordSet();
        //有可能存在冲销金额 因此实际申请的报销金额要减去冲销金额
        rs.execute("SELECT ysxm,t.sqbxje,t.tybxje,(t.tybxje-t.sqbxje )AS che FROM (\n" +
                " SELECT ysxm,SUM(sqbxje-ISNULL(cxje,0))AS sqbxje,SUM(tybxje-ISNULL(cxje,0))AS tybxje \n" +
                " FROM "+dtTable+" WHERE mainid="+mainid+"  GROUP BY  ysxm\n" +
                ") t");
        while (rs.next()) {
            Map<String, Object> data = new HashMap<>();
            String ysxm = rs.getString("ysxm");
            Double sqbxje = rs.getDouble("sqbxje");
            Double tybxje = rs.getDouble("tybxje");
            Double che = rs.getDouble("che");
            data.put("ysxm", ysxm);
            data.put("sqbxje", sqbxje);
            data.put("tybxje", tybxje);
            data.put("che", che);
            datas.add(data);
        }
        return datas;
    }

    /**
     * 会计审核后检查额度获取明细1报销项目数据 group by 合并同类项
     * @param dtTable
     * @param mainid
     * @return
     */
    public List<Map<String,Object>> getDetailsInfo_checkAudit(String dtTable,int mainid){
        List<Map<String,Object>> datas = new ArrayList<>();
        RecordSet rs = new RecordSet();
        //有可能存在冲销金额 因此实际申请的报销金额要减去冲销金额
        rs.execute("SELECT ysxm,bmjjzcfl,t.sqbxje,t.tybxje,(t.tybxje-t.sqbxje )AS che FROM (\n" +
                " SELECT ysxm,bmjjzcfl,SUM(sqbxje-ISNULL(cxje,0))AS sqbxje,SUM(tybxje-ISNULL(cxje,0))AS tybxje \n" +
                " FROM "+dtTable+" WHERE mainid="+mainid+"  GROUP BY  ysxm,bmjjzcfl\n" +
                ") t");
        while (rs.next()){
            Map<String,Object> data = new HashMap<>();
            data.put("ysxm",rs.getString("ysxm"));
            data.put("bmjjzcfl",rs.getString("bmjjzcfl"));
            data.put("sqbxje",rs.getDouble("sqbxje"));
            data.put("tybxje",rs.getDouble("tybxje"));
            data.put("che",rs.getDouble("che"));//差额
            datas.add(data);
        }
        return datas;
    }

    /**
     * 根据会计审核时的数据进行扣减或释放预算经济分类额度
     * @param rst
     * @param jjfldatas
     * @return
     * @throws Exception
     */
    public boolean reduceOrReleaseJingFei(RecordSetTrans rst,List<Map<String,Object>> jjfldatas) throws Exception {
        boolean result = false;
        for(int i=0;i<jjfldatas.size();i++){
            Map<String,Object> data = jjfldatas.get(i);
            Double che = (Double) data.get("che");
            String ysxm = data.get("ysxm").toString();
            String bmjjzcfl = data.get("bmjjzcfl").toString();
            boolean thisResult = false;
            if(che<0){
                //如果差额为正则需要多扣减，如果差额为负数则需要还回差额 取绝对值相加
                thisResult = rst.executeUpdate(releaseJingFeiSQL,Math.abs(che),Math.abs(che),ysxm,bmjjzcfl);
            }else{
                thisResult = rst.executeUpdate(reduceJingFeiSQL,che,che,ysxm,bmjjzcfl);
            }
            if(!thisResult){
                return false;
            }
            result = thisResult;
        }
        return result;
    }

    /**
     * 根据会计审核时的数据进行扣减或释放使用单位额度
     * @param rst
     * @param jjfldatas
     * @return
     * @throws Exception
     */
    public boolean reduceOrReleaseSydwed(RecordSetTrans rst,List<Map<String,Object>> jjfldatas,int subid) throws Exception {
        boolean result = false;
        for(int i=0;i<jjfldatas.size();i++){
            Map<String,Object> data = jjfldatas.get(i);
            Double che = (Double) data.get("che");
            String ysxm = data.get("ysxm").toString();
            boolean thisResult = false;
            if(che<0){
                //如果差额为正则需要多扣减，如果差额为负数则需要还回差额 取绝对值相加
                thisResult = rst.executeUpdate(releaseSydwedSQL,Math.abs(che),Math.abs(che),ysxm,subid);
            }else{
                thisResult = rst.executeUpdate(reduceSydwedSQL,che,che,ysxm,subid);
            }
            if(!thisResult){
                return false;
            }
            result = thisResult;
        }
        return result;
    }

    /**
     * 检查明细表报销时渠道的剩余额度是否足够
     * @param dtTable
     * @param mainid
     * @return
     */
    public Map<String,Object> checkAuditDetailsToQudao(String dtTable,int mainid){
        logTool.writeLog("会计审核时检查明细表报销时渠道的剩余额度是否足够");
        Map<String,Object> result = new HashMap<>();
        result.put("status",true);
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        rs1.execute("SELECT ysxm,zcqd,xzkmdm,fzysrfl,zcdfczbkxm,SUM(sqbxje-ISNULL(cxje,0))AS sqbxje,SUM(tybxje-ISNULL(cxje,0))AS tybxje,ysly\n" +
                "FROM "+dtTable+" WHERE mainid="+mainid+" GROUP BY  ysxm,zcqd,xzkmdm,fzysrfl,zcdfczbkxm,ysly");
        while (rs1.next()) {
            String ysxm = rs1.getString("ysxm");
            int zcqd = rs1.getInt("zcqd");// 0-财政拨款 1-非财政拨款
            String fzysrfl = rs1.getString("fzysrfl");
            String zcdfczbkxm = rs1.getString("zcdfczbkxm");//fzysrfl为5和8时 为uf_fzysrxm表id
            //Double sqbxje = rs1.getDouble("sqbxje");
            Double tybxje = rs1.getDouble("tybxje");//同意报销金额
            int ysly = rs1.getInt("ysly");// 0-当年预算 1-上年结转
            // 根据zcqd 分别从财政拨款收入和非财政拨款收入取值
            // 根据ysly 判断是取当年预算还是上年结转的金额字段
            String yslylx = (ysly == 0 ? "当年预算核批" : "上年结转核批");
            String baseSql = "select id,je,syed,djje,yslylx," +
                    "(select mc from uf_srysfl where id=zcqd)as zcqdmc,\n"+
                    "(select xmmc from uf_fzysrxm where id=dfczbkxm)as dfczbkxmmc \n" +
                    " from uf_jingfeilaiyuan where lcid="+ysxm+" and yslylx='"+yslylx+"' and srlx="+zcqd;
            logTool.writeLog("预算项目id:" + ysxm + ",支出渠道:" + zcqd + "预算来源:" + ysly + "非中央收入分类:" + fzysrfl);
            if(zcqd==0){
                //财政拨款
                int xzkmdm = rs1.getInt("xzkmdm");//从 uf_gxmdygnkm 取数据判断
                Map<String,String>  qudao = getQudaoYsly(xzkmdm);
                baseSql += " and zyczbksr='"+qudao.get("hpssjid")+"' ";
            }else{
                if(fzysrfl.equals("5") || fzysrfl.equals("8") ){
                    baseSql += " and zcqd="+fzysrfl+" and dfczbkxm="+zcdfczbkxm;
                }else{
                    baseSql += " and zcqd="+fzysrfl;
                }
            }
            logTool.writeLog(baseSql);
            if(rs2.execute(baseSql)){
                if(rs2.next()){
                    Double syed = rs2.getDouble("syed");
                    //2023年12月15日 负数冲销的不判断
                    if(tybxje > 0 && syed<tybxje){
                        String qdmc = zcqd==0
                                ?rs2.getString("yslylx")+"/"+new YuSuanTiaoZhengDao().getZyczbksrName(rs2.getString("zyczbksr"))
                                :rs2.getString("yslylx")+"/"+rs2.getString("zcqdmc")+ (fzysrfl.equals("5") || fzysrfl.equals("8")?"/"+rs2.getString("dfczbkxmmc"):"");
                        result.put("status",false);
                        result.put("msg","预算项目:" + getProjectInfo(ysxm,"xmmin") + "渠道["+qdmc+"]剩余额度:"+syed+"不足以扣减同意报销金额:"+tybxje);
                        return result;
                    }
                }else{
                    result.put("status",false);
                    result.put("msg","预算项目:" + getProjectInfo(ysxm,"xmmin") + "根据报销审核时的支出渠道及预算来源未查询到核批渠道");
                    return result;
                }
            }else{
                result.put("status",false);
                result.put("msg","预算项目:" + getProjectInfo(ysxm,"xmmin") + "根据报销审核时的支出渠道及预算来源查询核批渠道SQL失败");
                return result;
            }
        }
        return result;
    }

    /**
     * 会计审核时根据明细表报销时渠道冻结资金
     * @param rst
     * @param dtTable
     * @param mainid
     * @return
     * @throws Exception
     */
    public Map<String,Object> frozenJingFeiLaiYuan(RecordSetTrans rst,String dtTable,int mainid) throws Exception {
        logTool.writeLog("会计审核时根据明细表报销时渠道冻结资金");
        boolean flag = false;
        Map<String,Object> result = new HashMap<>();
        result.put("status",true);
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        rs1.execute("SELECT ysxm,zcqd,xzkmdm,fzysrfl,zcdfczbkxm,SUM(sqbxje-ISNULL(cxje,0))AS sqbxje,SUM(tybxje-ISNULL(cxje,0))AS tybxje,ysly\n" +
                "FROM "+dtTable+" WHERE mainid="+mainid+" GROUP BY  ysxm,zcqd,xzkmdm,fzysrfl,zcdfczbkxm,ysly");
        while (rs1.next()) {
            String ysxm = rs1.getString("ysxm");
            int zcqd = rs1.getInt("zcqd");// 0-财政拨款 1-非财政拨款
            String fzysrfl = rs1.getString("fzysrfl");
            String zcdfczbkxm = rs1.getString("zcdfczbkxm");//fzysrfl为5和8时 为uf_fzysrxm表id
            //Double sqbxje = rs1.getDouble("sqbxje");
            Double tybxje = rs1.getDouble("tybxje");//同意报销金额
            int ysly = rs1.getInt("ysly");// 0-当年预算 1-上年结转
            // 根据zcqd 分别从财政拨款收入和非财政拨款收入取值
            // 根据ysly 判断是取当年预算还是上年结转的金额字段
            String yslylx = (ysly == 0 ? "当年预算核批" : "上年结转核批");
            String getSql = "select id,je,syed,djje,yslylx," +
                    "(select mc from uf_srysfl where id=zcqd)as zcqdmc,\n"+
                    "(select xmmc from uf_fzysrxm where id=dfczbkxm)as dfczbkxmmc \n" +
                    " from uf_jingfeilaiyuan where lcid="+ysxm+" and yslylx='"+yslylx+"' and srlx="+zcqd;
            logTool.writeLog("预算项目id:" + ysxm + ",支出渠道:" + zcqd + "预算来源:" + ysly + "非中央收入分类:" + fzysrfl);
            String setSql = " update uf_jingfeilaiyuan set djje=ISNULL(djje,0)+?,syed=syed-? where lcid="+ysxm+" and yslylx='"+yslylx+"' and srlx="+zcqd;
            if(zcqd==0){
                //财政拨款
                int xzkmdm = rs1.getInt("xzkmdm");//从 uf_gxmdygnkm 取数据判断
                Map<String,String>  qudao = getQudaoYsly(xzkmdm);
                getSql += " and zyczbksr='"+qudao.get("hpssjid")+"' ";
                setSql += " and zyczbksr='"+qudao.get("hpssjid")+"' ";
                String xmlcid = qudao.get("xmlcid");
                if(!xmlcid.equals(ysxm)){
                    result.put("status",false);
                    result.put("msg","报销明细中的项目id["+ysxm+"]与核批渠道记录中的项目id["+xmlcid+"]不一致(预算来源校验)");
                    logTool.writeLog(JSONObject.toJSONString(result));
                    return result;
                }
            }else{
                if(fzysrfl.equals("5") || fzysrfl.equals("8") ){
                    getSql += " and zcqd="+fzysrfl+" and dfczbkxm="+zcdfczbkxm;
                    setSql += " and zcqd="+fzysrfl+" and dfczbkxm="+zcdfczbkxm;
                }else{
                    getSql += " and zcqd="+fzysrfl;
                    setSql += " and zcqd="+fzysrfl;
                }
            }
            //在前一步已经校验过getSql
            rs2.execute(getSql);
            rs2.next();
            logTool.writeLog("更新sql："+setSql);
            boolean thisResult = rst.executeUpdate(setSql,tybxje,tybxje);
            if(!thisResult){
                String qdmc = zcqd==0
                        ?rs2.getString("yslylx")+"/"+new YuSuanTiaoZhengDao().getZyczbksrName(rs2.getString("zyczbksr"))
                        :rs2.getString("yslylx")+"/"+rs2.getString("zcqdmc")+ (fzysrfl.equals("5") || fzysrfl.equals("8")?"/"+rs2.getString("dfczbkxmmc"):"");
                result.put("status",false);
                result.put("msg","预算项目:" + getProjectInfo(ysxm,"xmmin") + "渠道["+qdmc+"]冻结同意报销金额失败");
                logTool.writeLog(JSONObject.toJSONString(result));
                return result;
            }
            flag = thisResult;
        }
        return result;
    }

    /**
     * 根据科目下级id获取所有科目下级层级id（包含本身）
     * 例如行政运行-公用经费11-定员定额14-公用定项18 则返回11，14，18
     * @param id
     * @return
     */
    public List<String> getSuperiorId(Integer id){
        List<String> list = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = " select sjzxsr from  uf_zyczbksr_xj where id = "+id+" ";
        rs.executeQuery(sql);
        rs.next();
        if (StrUtil.isNotEmpty(rs.getString("sjzxsr"))){
            List<String> sjzxsr = getSuperiorId(rs.getInt("sjzxsr"));
            list.addAll(sjzxsr);
        }
        list.add(String.valueOf(id));
        return list;
    }

    /**
     * 根据科目下级的id获取对应科目级id
     * @param id
     * @return
     */
    public  RecordSet getZyczxj(Integer id){
        RecordSet rs=new RecordSet();
        String zyxjSql = "select szgnkm,srzx from uf_zyczbksr_xj where id=" + id;
        rs.executeQuery(zyxjSql);
        logTool.writeLog("查询中央财政收入下级所在科目级sql:"+zyxjSql);
        return rs;
    }

    /**
     * 会计审核时根据明细表报销冻结收入预算核批金额
     * 只针对财政拨款上年结转实际收入跟非财政拨款上年结转实际收入和当年实际收入
     * @param rst
     * @param dtTable
     * @param mainid
     * @return
     * @throws Exception
     */
    public Map<String,Object> frozenSrys(RecordSetTrans rst,String dtTable,int mainid) throws Exception {
        //4个收入表补充字段：以前年度实际结转冻结金额  非财政拨款收入补充实际收入冻结金额和实际收入报销金额
        logTool.writeLog("会计审核时根据明细表报销时冻结收入预算核批金额");
        boolean flag = false;
        Map<String,Object> result = new HashMap<>();
        result.put("status",true);
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        rs1.execute("SELECT ysxm,zcqd,xzkmdm,fzysrfl,zcdfczbkxm,SUM(sqbxje-ISNULL(cxje,0))AS sqbxje,SUM(tybxje-ISNULL(cxje,0))AS tybxje,ysly\n" +
                "FROM "+dtTable+" WHERE mainid="+mainid+" GROUP BY  ysxm,zcqd,xzkmdm,fzysrfl,zcdfczbkxm,ysly");
        while (rs1.next()) {
            String ysxm = rs1.getString("ysxm");
            int zcqd = rs1.getInt("zcqd");// 0-财政拨款 1-非财政拨款
            String fzysrfl = rs1.getString("fzysrfl");
            String zcdfczbkxm = rs1.getString("zcdfczbkxm");//fzysrfl为5和8时 为uf_fzysrxm表id
            //Double sqbxje = rs1.getDouble("sqbxje");
            Double tybxje = rs1.getDouble("tybxje");//同意报销金额
            int ysly = rs1.getInt("ysly");// 0-当年预算 1-上年结转
            // 根据zcqd 分别从财政拨款收入和非财政拨款收入取值
            // 根据ysly 判断是取当年预算还是上年结转的金额字段
            String yslylx = (ysly == 0 ? "当年预算核批" : "上年结转核批");
            String getSql = "select id,je,syed,djje,yslylx," +
                    "(select mc from uf_srysfl where id=zcqd)as zcqdmc,\n"+
                    "(select xmmc from uf_fzysrxm where id=dfczbkxm)as dfczbkxmmc \n" +
                    " from uf_jingfeilaiyuan where lcid="+ysxm+" and yslylx='"+yslylx+"' and srlx="+zcqd;
            logTool.writeLog("预算项目id:" + ysxm + ",支出渠道:" + zcqd + "预算来源:" + ysly + "非中央收入分类:" + fzysrfl);
            if(zcqd==0){
                //财政拨款
                int xzkmdm = rs1.getInt("xzkmdm");//从 uf_gxmdygnkm 取数据判断
                Map<String,String>  qudao = getQudaoYsly(xzkmdm);
                getSql += " and zyczbksr='"+qudao.get("hpssjid")+"' ";
                String xmlcid = qudao.get("xmlcid");
                if(!xmlcid.equals(ysxm)){
                    result.put("status",false);
                    result.put("msg","报销明细中的项目id["+ysxm+"]与核批渠道记录中的项目id["+xmlcid+"]不一致(收入预算核批校验)");
                    logTool.writeLog(JSONObject.toJSONString(result));
                    return result;
                }
            }else{
                if(fzysrfl.equals("5") || fzysrfl.equals("8") ){
                    getSql += " and zcqd="+fzysrfl+" and dfczbkxm="+zcdfczbkxm;
                }else{
                    getSql += " and zcqd="+fzysrfl;
                }
            }
            //在前一步已经校验过getSql
            rs2.execute(getSql);
            rs2.next();
            String qdmc = zcqd==0
                    ?rs2.getString("yslylx")+"/"+new YuSuanTiaoZhengDao().getZyczbksrName(rs2.getString("zyczbksr"))
                    :rs2.getString("yslylx")+"/"+rs2.getString("zcqdmc")+ (fzysrfl.equals("5") || fzysrfl.equals("8")?"/"+rs2.getString("dfczbkxmmc"):"");
            logTool.writeLog("项目id"+ysxm+" 支出渠道："+qdmc);
            String setSql = "";//更新收入预算核批剩余金额的SQL
            boolean thisResult = false;
            if (zcqd == 0) {
                //支出渠道-中央财政收入,收入类型-中央财政收入，中央科目级和下级的扣减
                //财政拨款
                int xzkmdm = rs1.getInt("xzkmdm");//从 uf_gxmdygnkm 取数据判断
                Map<String,String>  qudao = getQudaoYsly(xzkmdm);
                BaseBean baseBean = new BaseBean();
                int yj = Integer.parseInt(baseBean.getPropValue("Verification","yj"));//科目级
                int xj = Integer.parseInt(baseBean.getPropValue("Verification","xj"));//下级
                int type = 0;//判断科目级还是下级
                int dataid = 0;//数据id
                String zyczbksr = qudao.get("hpssjid");
                //logTool.writeLog("中央财政拨款收入_核批时数据id:"+zyczbksr+"科目级/下级:"+kmjxj);
                if (StringUtils.isNotBlank(zyczbksr)) {
                    String[] sta = zyczbksr.split("_");//当年结转核批下划线分割
                    type = Integer.parseInt(sta[0]);//9为科目级，10为科目下级
                    dataid = Integer.parseInt(sta[1]);//表id
                    if (type == yj) {
                        //当年预算
                        if(ysly==0){
                            //冻结金额增加 核批剩余金额减少
                            setSql = "update uf_zyczbksr_km set djje=isnull(djje,0)+("+tybxje+"),hpsyje=hpsyje-("+tybxje+") where id in("+dataid+")";
                            logTool.writeLog("当年预算财政拨款科目级冻结SQL："+setSql);
                        }else{
                            //上年结转核批冻结金额增加 上年结转核批剩余金额减少
                            setSql = "update uf_zyczbksr_km set snjzhpdjje=isnull(snjzhpdjje,0)+("+tybxje+"),snjzhpsyje=snjzhpsyje-("+tybxje+") where id in("+dataid+")";
                            logTool.writeLog("上年结转预算财政拨款科目级冻结SQL："+setSql);
                        }
                        thisResult = rst.executeUpdate(setSql);
                        if(!thisResult){
                            result.put("status",false);
                            result.put("msg","预算项目:" + getProjectInfo(ysxm,"xmmin") + "渠道["+qdmc+"]冻结收入预算科目级核批剩余金额失败");
                            logTool.writeLog(JSONObject.toJSONString(result));
                            return result;
                        }
                    } else if (type == xj) {
                        List<String> superiorId = getSuperiorId(dataid);//获取包含本身的所有id
                        String joinId = Joiner.on(",").join((Iterable<?>) superiorId);
                        //还要再扣减对应的科目级
                        RecordSet zyczxj = getZyczxj(dataid);
                        zyczxj.next();
                        Integer superid = zyczxj.getInt("szgnkm");
                        String superSql = "";
                        if(ysly==0){
                            //冻结金额增加 核批剩余金额减少
                            setSql = "update uf_zyczbksr_xj set djje=isnull(djje,0)+("+tybxje+"),yhpsy=yhpsy-("+tybxje+") where id in("+joinId+")";
                            logTool.writeLog("当年预算财政拨款科目下级冻结SQL："+setSql);
                            superSql = "update uf_zyczbksr_km set djje=isnull(djje,0)+("+tybxje+"),hpsyje=hpsyje-("+tybxje+") where id in("+superid+")";;
                        }else{
                            //上年结转核批冻结金额增加 上年结转核批剩余金额减少
                            setSql = "update uf_zyczbksr_xj set snjzhpdjje=isnull(snjzhpdjje,0)+("+tybxje+"),snjzhpsyje=snjzhpsyje-("+tybxje+") where id in("+joinId+")";
                            logTool.writeLog("上年结转预算财政拨款科目下级冻结SQL："+setSql);
                            superSql = "update uf_zyczbksr_km set snjzhpdjje=isnull(snjzhpdjje,0)+("+tybxje+"),snjzhpsyje=snjzhpsyje-("+tybxje+") where id in("+superid+")";
                        }
                        thisResult = rst.executeUpdate(setSql);
                        if(!thisResult){
                            result.put("status",false);
                            result.put("msg","预算项目:" + getProjectInfo(ysxm,"xmmin") + "渠道["+qdmc+"]冻结收入预算科目下级核批剩余金额失败");
                            logTool.writeLog(JSONObject.toJSONString(result));
                            return result;
                        }
                        //对应科目级的更新
                        thisResult = rst.executeUpdate(superSql);
                        if(!thisResult){
                            result.put("status",false);
                            result.put("msg","预算项目:" + getProjectInfo(ysxm,"xmmin") + "渠道["+qdmc+"]冻结收入预算科目下级对应科目级核批剩余金额失败");
                            logTool.writeLog(JSONObject.toJSONString(result));
                            return result;
                        }
                    }
                }
            } else if (zcqd == 1 && fzysrfl.equals("8") || zcqd == 1 && fzysrfl.equals("5")) {//非中央财政收入-财政拨款
                //冻结地方财政拨款项目或其他收入

            } else if (zcqd == 1) {
                //冻结地方财政拨款

            }
            flag = thisResult;
        }
        return result;
    }
}
